﻿using System.Collections.Generic;
using DtoModels;
using Entities;
using Microsoft.AspNetCore.Mvc;
using Portal.Filter;
using Portal.Models;
using SqlSugar;

namespace Portal.Controllers
{
    /// <summary>
    /// 菜单管理 业务逻辑入口
    /// </summary>
    [LoginCheckFilter(IsCheck = true)]
    public class MenuController : Controller
    {
        private readonly SqlSugarClient _sqlSugarClient;

        public MenuController(SqlSugarClient sqlSugarClient)
        {
            _sqlSugarClient = sqlSugarClient;
        }

        /// <summary>
        /// 菜单管理
        /// </summary>
        /// <returns></returns>
        public ActionResult LoadMenuManagerLeftTreeJson()
        {
            var queryResult = this._sqlSugarClient.Context.Queryable<sys_permission>().Where(x => x.type == GlobalVariable.TypeMenu).ToList();
            var list = new List<DTreeResDto>();

            foreach (var item in queryResult)
            {
                list.Add(new DTreeResDto
                {
                    Id = item.id,
                    ParentId = item.pid,
                    Title = item.title,
                    CheckArr = item.open.ToString()
                });
            }

            var result = new LayUiStandardResDto<DTreeResDto>
            {
                Code = 0,
                Msg = "查询成功",
                Data = list
            };

            return Json(result);
        }

        /// <summary>
        /// 加载所有的菜单
        /// </summary>
        /// <returns></returns>
        public ActionResult LoadAllMenu(MenuInfoReqDto menuInfoReqDto)
        {
            var totalCount = 0;

            var queryWhere = this._sqlSugarClient.Queryable<sys_permission>().Where(x => x.type == GlobalVariable.TypeMenu);

            //ContainsArrayUseSqlParameters

            if (!string.IsNullOrEmpty(menuInfoReqDto.Title))
            {
                queryWhere = queryWhere.Where(x => x.title.Contains(menuInfoReqDto.Title));
            }


            if (menuInfoReqDto.Id != 0)
            {
                queryWhere = queryWhere.Where(x => x.id == menuInfoReqDto.Id);
            }

            

            var queryResult = queryWhere.ToPageList(menuInfoReqDto.Page, menuInfoReqDto.Limit, ref totalCount);

            var result = new LayUiStandardResDto<sys_permission>
            {
                Code = 0,
                Msg = "查询成功",
                Count = totalCount,
                Data = queryResult
            };

            return Json(result);
        }

        /// <summary>
        /// 获取当前最大的菜单排序码
        /// </summary>
        /// <returns></returns>
        public ActionResult LoadMenuMaxOrderNum()
        {
            var maxOrderNum = this._sqlSugarClient.Queryable<sys_permission>().Where(x => x.type == GlobalVariable.TypeMenu).Max(x=>x.ordernum);
            return Json(new { value = (maxOrderNum + 1) });
        }


        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="permission"></param>
        /// <returns></returns>
        public ActionResult AddMenu(sys_permission permission)
        {
            permission.type = GlobalVariable.TypeMenu;
            var insertNum = this._sqlSugarClient.Insertable<sys_permission>(permission).ExecuteCommand();

            var result = new LayUiStandardResDto<object>
            {
                Code = GlobalVariable.Ok,
                Msg = $"成功添加{insertNum}菜单"
            };

            return Json(result);
        }

        /// <summary>
        /// 更新菜单
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateMenu(sys_permission permission)
        {
            permission.type = GlobalVariable.TypeMenu;
            var updateNum = this._sqlSugarClient.Updateable<sys_permission>(permission).ExecuteCommand();
            var result = new LayUiStandardResDto<object>
            {
                Code = GlobalVariable.Ok,
                Msg = $"成功更新{updateNum}菜单"
            };

            return Json(result);
        }

        /// <summary>
        /// 判断如果当前菜单存在子菜单就不允许删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult CheckMenuHasChildrenNode(int id)
        {
            var count = this._sqlSugarClient.Queryable<sys_permission>().Count(x => x.pid == id);

            if (count > 0)  //存在子菜单
            {
                return Json(new { value = true });
            }

            return Json(new { value = false });
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult DeleteMenu(int id)
        {
            var deleteNum = this._sqlSugarClient.Deleteable<sys_permission>(x => x.id == id).ExecuteCommand();
            var result = new LayUiStandardResDto<object>
            {
                Code = GlobalVariable.Ok,
                Msg = $"成功删除了{deleteNum}个菜单"
            };

            return Json(result);
        }
    }
}